<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property string $id
 * @property string $category
 * @property string $category_ids
 * @property string $brand_id
 * @property string $product_name
 * @property string $url_key
 * @property string $product_code
 * @property string $old_price
 * @property string $new_price
 * @property string $last_update
 * @property string $content
 * @property string $hits
 * @property integer $is_hot
 * @property string $tagged
 * @property string $sorder
 * @property integer $status
 * @property integer $blogspot
 */
class Product extends ProductPeer
{
	var $className = __CLASS__;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('product_name, url_key', 'required'),
			array('is_hot, status, blogspot', 'numerical', 'integerOnly'=>true),
			array('category, brand_id, last_update, hits, sorder', 'length', 'max'=>20),
			array('category_ids, product_name, url_key, tagged', 'length', 'max'=>300),
			array('product_code, old_price, new_price', 'length', 'max'=>100),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category, category_ids, brand_id, product_name, url_key, product_code, old_price, new_price, last_update, content, hits, is_hot, is_new, tagged, sorder, status, blogspot', 'safe', 'on'=>'search'),
		);
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);

		$criteria->compare('category',$this->category,true);

		$criteria->compare('category_ids',$this->category_ids,true);

		$criteria->compare('brand_id',$this->brand_id,true);

		$criteria->compare('product_name',$this->product_name,true);

		$criteria->compare('url_key',$this->url_key,true);

		$criteria->compare('product_code',$this->product_code,true);

		$criteria->compare('old_price',$this->old_price,true);

		$criteria->compare('new_price',$this->new_price,true);

		$criteria->compare('last_update',$this->last_update,true);

		$criteria->compare('content',$this->content,true);

		$criteria->compare('hits',$this->hits,true);

		$criteria->compare('is_hot',$this->is_hot);

		$criteria->compare('tagged',$this->tagged,true);

		$criteria->compare('sorder',$this->sorder,true);

		$criteria->compare('status',$this->status);

		$criteria->compare('blogspot',$this->blogspot);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
    public function rebuildSOrder() {
        $criteria=new CDbCriteria;
        $criteria->select = "id, sorder";
        $criteria->order = "sorder, id";
        $data = $this->model()->findAll($criteria);
        $row_numbers = count($data);
        $sorder = 0;
        for($i=0; $i<$row_numbers-1; $i++) {
            $sorder++;
            self::model()->updateByPk($data[$i]->id, array('sorder'=>$sorder));
        }
        $sorder++;
        self::model()->updateByPk($data[$i]->id, array('sorder'=>$sorder));
    }
    public function buildUrlKey() {
        if($this->url_key == "") $this->url_key = Common::makeFriendlyUrl($this->name, true);
        else $this->url_key = Common::makeFriendlyUrl($this->url_key, true);
        $record = Lvc::selectOne($this->tableName(), 'id', 'url_key="'.$this->url_key.'" ');
        if($record)
            $this->url_key .= uniqid();
    }
    public function getImageDir($id = '')
    {
        if($id>0) $this->id = $id;
        $imageDir = _APP_PATH_.DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."product".DIRECTORY_SEPARATOR.$this->id;
        if(!is_dir($imageDir)) mkdir($imageDir, 0777, true);
        return $imageDir;
    }
    public function getImageUrl($imgId, $type='_normal',$ext)
    {
        $url = "";
        if($imgId>0){
            $root = Yii::app()->params->staticHost."/data/product/".$this->id."/";
            $url = $root.$imgId.$type.$ext;
        }
        return $url;
    }
    public function getDefaultImage()
    {
        $imageDir = self::model()->getImageDir($this->id);
        $image = ProductImagePeer::model()->findByAttributes(array('product_id'=>$this->getId(), 'is_default'=>1));
        $defaultImage = null;
        if($image && $image->is_default){
            $item = new stdClass();
            $item->id = $image->id;
            $item->name = $image->name;
            $item->is_default = $image->is_default;
            $item->small = $this->getImageUrl($image->id,'_small',$image->file_name);
            $item->thumb = $this->getImageUrl($image->id, '_thumbnail', $image->file_name);
            $item->medium = $this->getImageUrl($image->id, '_medium',$image->file_name);
            $item->normal = $this->getImageUrl($image->id, '_normal',$image->file_name);
			
            $item->large = $item->normal;
            if(file_exists($imageDir.DIRECTORY_SEPARATOR.$image->id.'_large'.$ext))
                $item->large = $this->getImageUrl($image->id, 'large', $ext);
            $defaultImage = $item;
        }
        return $defaultImage;
    }
}